package gson.emlog;

import gson.emlog.activity.Blogs;
import gson.emlog.activity.Cloud;
import gson.emlog.activity.Comments;
import gson.emlog.activity.Link;
import gson.emlog.activity.Setting;
import gson.emlog.activity.Sort;
import gson.emlog.activity.Twitter;
import gson.emlog.activity.Write;
import gson.emlog.utils.Constant;
import gson.emlog.utils.Menu;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class Emlog extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences settings = this.getSharedPreferences(Constant.PREFS_NAME, 0);
        String url = settings.getString(Constant.PREFS_KEY_BLOG_URL, "");
        String appkey =  settings.getString(Constant.PREFS_KEY_APP_KEY, "");
        String clintkey = settings.getString(Constant.PREFS_KEY_CLIENT_KEY, "");
        if(url == "" || appkey == "" || clintkey == ""){
        	startActivity(new Intent(Emlog.this,Setting.class));
        	this.finish();
        }else{
        setContentView(R.layout.main);
        
        GridView gdv = (GridView) findViewById(R.id.GridView);
        Menu menu = Menu.getInstance(getResources());
		
        SimpleAdapter saItem = new SimpleAdapter(this, menu.getMenu(), // 数据源
				R.layout.item, // xml实现
				new String[] { "icon", "text" }, // 对应map的Key
				new int[] { R.id.ItemImage, R.id.ItemText }); // 对应R的Id

		// 添加Item到网格中
		gdv.setAdapter(saItem);
		// 添加点击事件
		gdv.setOnItemClickListener(new OnItemClickListener() {
			@SuppressWarnings("unchecked")
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				HashMap<String, Object> item = (HashMap<String, Object>) arg0.getAdapter().getItem(arg2);
				
				String itemText = item.get("text").toString();
				Resources rs = getResources();
				if(itemText.equals(rs.getString(R.string.write))){
					startActivity(new Intent(Emlog.this,Write.class));
				}else if(itemText.equals(rs.getString(R.string.logs))){
					startActivity(new Intent(Emlog.this,Blogs.class));
				}else if(itemText.equals(rs.getString(R.string.twitter))){
					startActivity(new Intent(Emlog.this,Twitter.class));
				}else if(itemText.equals(rs.getString(R.string.sort))){
					startActivity(new Intent(Emlog.this,Sort.class));
				}else if(itemText.equals(rs.getString(R.string.comment))){
					startActivity(new Intent(Emlog.this,Comments.class));
				}else if(itemText.equals(rs.getString(R.string.cloud))){
					startActivity(new Intent(Emlog.this,Cloud.class));
				}else if(itemText.equals(rs.getString(R.string.flink))){
					startActivity(new Intent(Emlog.this,Link.class));
				}else if(itemText.equals(rs.getString(R.string.setting))){
					startActivity(new Intent(Emlog.this,Setting.class));
				}
				
				
				overridePendingTransition(R.anim.fade, R.anim.hold);
			}
		});
		
		/*Button write = (Button) findViewById(R.id.write);
		write.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(Emlog.this,Write.class));
				overridePendingTransition(R.anim.fade, R.anim.hold);
			}
		});
		
		Button twitter = (Button) findViewById(R.id.twitter);
		twitter.setOnClickListener(twitterBtnOnClickListener);*/
        }
    }
    
    /*private View.OnClickListener twitterBtnOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			Builder alert = new AlertDialog.Builder(Emlog.this);
			EditText tucao = new EditText(Emlog.this);
			tucao.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
			tucao.setSingleLine(false);
			tucao.setGravity(Gravity.TOP);
			tucao.setMaxLines(5);
			alert.setTitle(getResources().getString(R.string.tucao));
			alert.setView(tucao);
			alert.setPositiveButton("确认", null);
			alert.setNegativeButton("取消", null);
			alert.show();
		}
	};*/
    
    @Override
    public void onBackPressed() {
    	super.onBackPressed();
    	this.finish();
    	System.exit(0);
    	android.os.Process.killProcess(android.os.Process.myPid());
    }
}